﻿using System.Collections.Generic;
using System.Data;
using Smatime.Web.DAO.SmatimeDatasetTableAdapters;
using Smatime.Web.Models;
using System.Linq;

namespace Smatime.Web.DAO
{
    public class StaffStoreRepository :
        BaseRepository<StaffStore, SmatimeDataset.StaffStoreDataTable, StaffStoreTableAdapter>
    {
        /// <summary>
        ///     Return the StaffStoreId by StaffID, storeId
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StaffStore GetStaffStoreId(long staffId, long storeId)
        {
            using (var adapter = new StaffStoreTableAdapter())
            {
                var list = new List<StaffStore>();
                using (
                    var table = adapter.GetDataByComanyStaffandCompanyStore(staffId,
                        storeId))
                {
                    var row = table.Rows[0];
                    return (StaffStore) (new StaffStore().FromDataRow(row));
                }
            }
        }

        /// <summary>
        ///     Get all store belong to staff by company staffId
        /// </summary>
        /// <returns></returns>
        public IEnumerable<StaffStore> GetStoreByStaffId(long companyStaffId)
        {
            using (var adapter = new StaffStoreTableAdapter())
            {
                using (var table = adapter.GetDataByCompanyStaffId(companyStaffId))
                {
                    var list = new List<StaffStore>();
                    foreach (DataRow row in table.Rows)
                    {
                        var storeOfStaff = new StaffStore();
                        storeOfStaff.FromDataRow(row);
                        list.Add(storeOfStaff);
                    }
                    return list;
                }
            }
        }

        public void UpdateStoreListByStaff(long companyStaffId, long[] storeIds)
        {
            using (var adapter = new StaffStoreTableAdapter())
            {
                using (var staffStoreTable = adapter.GetDataByCompanyStaffId(companyStaffId))
                {
                    //Delete all rows that not exist on storeIds array
                    var allRows = staffStoreTable.Rows.Cast<SmatimeDataset.StaffStoreRow>();
                    foreach (var row in allRows)
                    {
                        if (!storeIds.Contains(row.CompanyStoreId))
                            row.Delete();
                    }

                    //Add all staff-store pairs that not exist in the table
                    foreach (var storeId in storeIds)
                    {
                        var staffStoreRows = staffStoreTable.Select(string.Format("CompanyStaffId={0} and CompanyStoreId={1}", companyStaffId, storeId));
                        if (staffStoreRows == null || staffStoreRows.Length == 0)
                        {
                            staffStoreTable.AddStaffStoreRow(companyStaffId, storeId);
                        }
                    }

                    adapter.Update(staffStoreTable);
                }
            }
        }
    }
}